package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.CustomerDialog;

public class CustomerSearchFragment extends Fragment implements OnClickListener {

  public static final String EXTRA_NAME = "com.isunland.managesystem.entity.EXTRA_NAME";
  public static final String EXTRA_AREACODE = "com.isunland.managesystem.entity.EXTRA_AREACODE";
  public static final String EXTRA_AREANAME = "com.isunland.managesystem.entity.EXTRA_AREANAME";
  public static final String EXTRA_STATUS = "com.isunland.managesystem.entity.EXTRA_STATUS";
  //	private static final int START_DATE=0;
  private static final int AREA = 1;
  private static final int DATA_STATUS = 2;
  private BaseVolleyActivity mActivity;
  private EditText etCustomerName;
  private TextView tvCustomerEar;
  private TextView tvDataStatus;
  private ImageView ibCustomerEar;
  private ImageView ibDataStatus;

  private String dataStaus = "";
  private String dataStausId = "";
  private String mDistrictName = "";//地域名称
  private String mDistrictCode = "";//地域编码
  private String customerName = "";//客户名称

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.topicQuery);
    mActivity = (BaseVolleyActivity) getActivity();

    dataStaus = getArguments().getString(EXTRA_STATUS);
    customerName = getArguments().getString(EXTRA_NAME);
    mDistrictCode = getArguments().getString(EXTRA_AREACODE);
    mDistrictName = getArguments().getString(EXTRA_AREANAME);
    super.onCreate(savedInstanceState);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_customre_search, container, false);

    etCustomerName = (EditText) v.findViewById(R.id.et_customer_name);
    tvCustomerEar = (TextView) v.findViewById(R.id.tv_customer_ear);
    tvDataStatus = (TextView) v.findViewById(R.id.tv_data_status);

    ibCustomerEar = (ImageView) v.findViewById(R.id.ib_customer_area);
    ibDataStatus = (ImageView) v.findViewById(R.id.ib_data_status);

    ibCustomerEar.setOnClickListener(this);
    ibDataStatus.setOnClickListener(this);

    //初始化
    if (!TextUtils.isEmpty(customerName)) {
      etCustomerName.setText(customerName);
    }

    tvCustomerEar.setText(mDistrictName);
    //数据状态
    getStatus(dataStaus);
    return v;
  }

  private void getStatus(String dataStaus2) {
    if ("new".equals(dataStaus)) {
      tvDataStatus.setText(R.string.draft);//数据状态
    } else if ("submit".equals(dataStaus)) {
      tvDataStatus.setText(R.string.already_submit);//数据状态
    } else if ("publish".equals(dataStaus)) {
      tvDataStatus.setText(R.string.hasAudited);//数据状态
    } else if ("newback".equals(dataStaus)) {
      tvDataStatus.setText(R.string.removeCheck);//返回修改
    } else {
      tvDataStatus.setText(R.string.all);//全部
    }
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;
      //查询
      case R.id.menu_item_confirm:

        volleyPoastSearch();
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  //按条件查询订单
  private void volleyPoastSearch() {

    Intent i = new Intent();
    i.putExtra("districtCode", mDistrictCode);
    i.putExtra("districtDesc", mDistrictName);
    i.putExtra("dataStatus", dataStaus);
    if ("-未指定-".equals(etCustomerName.getText().toString())) {
      i.putExtra("customerName", "");
    } else {
      i.putExtra("customerName", etCustomerName.getText().toString());
    }

    getActivity().setResult(Activity.RESULT_OK, i);
    getActivity().finish();
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      case R.id.ib_customer_area:
        showDialog(AREA);
        break;
      case R.id.ib_data_status:
        showDialog(DATA_STATUS);
        break;

      default:
        break;
    }
  }

  private void showDialog(int requestCode) {

    DialogFragment dialog = null;
    switch (requestCode) {
      case AREA:
        dialog = new DistrictDialogFragment();
        break;

      //数据状态
      case DATA_STATUS:
        dialog = new CustomerSearchDialogFragment();
        break;

      default:
        break;
    }
    FragmentManager fm = getActivity().getSupportFragmentManager();
    dialog.setTargetFragment(CustomerSearchFragment.this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == AREA) {
      //			Date startDate = (Date) data
      //					.getSerializableExtra(BaseTimeDialogFragment.EXTRA_DATE);
      //			mstartDate = MyDateUtil.dateToString(startDate, MyDateUtil.TYPE_DATE);
      //			tvStartDate.setText(mstartDate);

      CustomerDialog customerDialog =
          (CustomerDialog) data.getSerializableExtra(DistrictDialogFragment.EXTRA_VALUE);
      tvCustomerEar.setText(customerDialog.getName());
      mDistrictName = customerDialog.getName();
      mDistrictCode = customerDialog.getId();
      return;
    }

    if (requestCode == DATA_STATUS) {
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(OrderSearchDialogFragment.EXTRA_VALUE);
      dataStaus = customer.getName();
      dataStausId = customer.getId();
      //初始化数据状态
      getStatus(dataStaus);

      return;
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  public static CustomerSearchFragment newInstance(String districtCode, String districtDesc,
      String name, String status) {

    Bundle args = new Bundle();
    args.putSerializable(EXTRA_AREACODE, districtCode);
    args.putSerializable(EXTRA_AREANAME, districtDesc);
    args.putSerializable(EXTRA_NAME, name);
    args.putSerializable(EXTRA_STATUS, status);
    CustomerSearchFragment fragment = new CustomerSearchFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
